package persistencia.daos;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

import persistencia.consultas.Consultas;
import persistencia.poolConexiones.IConexion;
import logica.Duenio;
import logica.excepciones.ExcepPersistencia;
import logica.valueObjects.VODuenio;
import persistencia.poolConexiones.Conexion;

public class DAODuenios implements IDAODuenios{
	
	public DAODuenios() {
		
	}
	
	public boolean member(IConexion icon, int ced) throws ExcepPersistencia{
		boolean existe = false;
		Conexion conex = (Conexion)icon;
		Connection con = null;
		Consultas cons = new Consultas();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try{
			con = conex.getConexion();
			pstmt = con.prepareStatement(cons.existeDuenio());
			pstmt.setInt(1, ced);
			rs = pstmt.executeQuery();
			if(rs.next())
				existe = true;
		}catch(SQLException e){
			throw new ExcepPersistencia(e.getMessage());
		}finally{
			if(rs != null)
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					throw new ExcepPersistencia(e.getMessage());
				}
			if(pstmt != null)
				try {
					pstmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					throw new ExcepPersistencia(e.getMessage());
				}
		}
		
		return existe;
	}
	
	public Duenio find(IConexion icon, int ced) throws ExcepPersistencia{
		Consultas cons = new Consultas();
		Conexion conex = (Conexion)icon;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		Duenio du = null;
		try{
			con = conex.getConexion();
			pstmt = con.prepareStatement(cons.obtenerDuenio());
			pstmt.setInt(1, ced);
			rs = pstmt.executeQuery();
			while(rs.next()){
				du = new Duenio(rs.getInt("cedula"), rs.getString("nombre"), rs.getString("apellido"));
			}
		}catch(SQLException e){
			throw new ExcepPersistencia(e.getMessage());
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ExcepPersistencia(e.getMessage());
		}finally{
			if(rs != null)
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					throw new ExcepPersistencia(e.getMessage());
				}
			if(pstmt != null)
				try {
					pstmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					throw new ExcepPersistencia(e.getMessage());
				}
		}
		
		return du;
		
	}
	
	public void insert(IConexion icon, Duenio duenio) throws ExcepPersistencia{
		int ced = duenio.getCedula();
		Conexion conex = (Conexion)icon;
		Connection con = null;
		String nom = duenio.getNombre();
		String ape = duenio.getApellido();
		Consultas cons = new Consultas();
		PreparedStatement pstmt = null;
		try{
			con = conex.getConexion();
			pstmt = con.prepareStatement(cons.nuevoDuenio());
			pstmt.setInt(1, ced);
			pstmt.setString(2, nom);
			pstmt.setString(3, ape);
			pstmt.executeUpdate();
		}catch(SQLException e){
			throw new ExcepPersistencia(e.getStackTrace().toString());
		}finally{
			if(pstmt != null)
				try {
					pstmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					throw new ExcepPersistencia(e.getMessage());
				}
		}		
	}
	
	public void delete(IConexion icon, int ced) throws ExcepPersistencia{
		Consultas cons = new Consultas();
		Conexion conex = (Conexion)icon;
		Connection con = null;
		PreparedStatement pstmt = null;
		try{
			con = conex.getConexion();
			Duenio duenio = find(icon, ced);
			duenio.borrarMascotas(icon);
			pstmt = con.prepareStatement(cons.borrarDuenioMascotas_D());
			pstmt.setInt(1, ced);
			pstmt.executeUpdate();
		}catch(SQLException e){
			throw new ExcepPersistencia(e.getMessage());
		}finally{
			if(pstmt != null)
				try {
					pstmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					throw new ExcepPersistencia(e.getMessage());
				}
		}		
	}
	
	public List<VODuenio> listarDuenios(IConexion icon) throws ExcepPersistencia{
		Consultas cons = new Consultas();
		Conexion conex = (Conexion)icon;
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		List<VODuenio> lista = new LinkedList<VODuenio>();
		try{
			con = conex.getConexion();
			stmt = con.createStatement();
			rs = stmt.executeQuery(cons.listarDuenios());
			while(rs.next()){
				VODuenio du = new VODuenio(rs.getInt("cedula"), rs.getString("nombre"), rs.getString("apellido"));
				lista.add(du);
			}
		}catch(SQLException e){
			throw new ExcepPersistencia(e.getMessage());
		}finally{
			if(rs != null)
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					throw new ExcepPersistencia(e.getMessage());
				}
			if(stmt != null)
				try {
					stmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					throw new ExcepPersistencia(e.getMessage());
				}
		}
		
		return lista;
		
	}

}
